package com.fxp.project.jiuselu.commons.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtractImgUrlUtils {

    public static String getFirstImgUrl(String str) {
        if(StringUtils.isBlank(str)){
            return "";
        }
        Pattern p_image = Pattern.compile("<img.*src\\s*=\\s*(.*?)[^>]*?>", Pattern.CASE_INSENSITIVE);
        Pattern r_image = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)");
        Matcher m_image = p_image.matcher(str);
        while (m_image.find()) {
            String img = m_image.group();
            System.out.println(img);
            Matcher m = r_image.matcher(img);
            while (m.find()) {
                return m.group(1);
            }
        }
        return "";
    }
}
